package com.xhwl.centiotdata.pollingtrigger.visitor;

import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationFactory;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.visitor.VisitorIntegrationConfigDTO;
import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.integrateconfig.VisitorIntegrationConfig;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.pojo.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 9:54 2022/6/13
 */
@Slf4j
@Component
public class VisitorDispatcher implements ScheduleDispatcher {

    @Autowired
    VisitorIntegrationConfig visitorIntegrationConfig;

    @Override
    public void doOneDayTask() {
        ThreadPoolUtils.executor.execute(this::executePullDeviceGroup);
        // 蒙牛访客VR获取访客体验数据
        ThreadPoolUtils.executor.execute(() ->this.executePullVisitorVrRecord(ProductEnum.YAOAN_VR.getId()));
        ThreadPoolUtils.executor.execute(this::executePullVisitorPrivilegeGroup);

    }

    public void executePullVisitorVrRecord(Integer productId){
        Map<Integer,List<VisitorIntegrationConfigDTO>> integrationConfig = visitorIntegrationConfig.getIntegrationConfig();
        integrationConfig.forEach((projectId, integrationConfigs) -> {
            integrationConfigs.forEach(integrationConfigDTO -> {
                try {
                    VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(integrationConfigDTO.getProductId());
                    if (handler != null && productId.equals(integrationConfigDTO.getProductId())) {
                        log.warn("开始拉取访客VR数据");
                        handler.getVisitorVrRecord(integrationConfigDTO);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        });
    }

    public void executePullDeviceGroup(){
        Map<Integer, List<VisitorIntegrationConfigDTO>> integrationConfig = visitorIntegrationConfig.getIntegrationConfig();
        integrationConfig.forEach((projectId, integrationConfigs) -> {
            integrationConfigs.forEach(integrationConfigDTO -> {
                try {
                    VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(integrationConfigDTO.getProductId());
                    if (handler == null) {
                        return;
                    }
                    handler.pullPrivilegeGroup(integrationConfigDTO);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        });
    }
    public void executePullVisitorPrivilegeGroup(){
        Map<Integer, List<VisitorIntegrationConfigDTO>> integrationConfig = visitorIntegrationConfig.getIntegrationConfig();
        integrationConfig.forEach((projectId, integrationConfigs) -> {
            integrationConfigs.forEach(integrationConfigDTO -> {
                try {
                    VisitorIntegrationHandler handler = VisitorIntegrationFactory.getInvokeStrategy(integrationConfigDTO.getProductId());
                    if (handler == null) {
                        return;
                    }
                    handler.pullVisitorPrivilegeGroup(integrationConfigDTO,null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        });
    }

}
